use once_cell::sync::Lazy;
use ssh2::Session;
use std::{collections::HashMap, net::TcpStream, sync::Mutex};
use tauri::State;

#[derive(Default)]
pub struct SshMap(pub Mutex<HashMap<String, Session>>);

#[tauri::command]
pub fn connect(state: State<SshMap>, addr: String) -> bool {
  let tcp = TcpStream::connect(&addr).unwrap();
  let mut session = Session::new().unwrap();
  session.set_tcp_stream(tcp);
  session.handshake().unwrap();

  session.userauth_password("username", "password").unwrap();

  state.0.lock().unwrap().insert(addr.clone(), session);
  true
}

static SSH_MAP: Lazy<Mutex<HashMap<String, Session>>> =
  Lazy::new(|| -> Mutex<HashMap<String, Session>> { Mutex::new(Default::default()) });

#[tauri::command]
pub async fn run() {
  println!("{:?}", SSH_MAP.lock().unwrap().keys());
}
